package com.novacloud.core;

import com.novacloud.exception.BaseException;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.sql.SQLException;

/**
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version 1.0
 * @date 2017/9/22 15:18
 */

public class BaseController {

    // 返回成功数据
    public Result resultSuccess(Object data) {
        Result result = new Result();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMessage(ResultCode.SUCCESS.message);
        result.setData(data);
        return result;
    }

    public Result resultFailure() {
        Result result = new Result();
        result.setCode(ResultCode.FAIL.code);
        result.setMessage(ResultCode.FAIL.message);
        return result;
    }

    public Result resultError() {
        Result result = new Result();
        result.setCode(ResultCode.INTERNAL_SERVER_ERROR.code);
        result.setMessage(ResultCode.INTERNAL_SERVER_ERROR.message);
        return result;
    }

    @ExceptionHandler(BaseException.class)
    public Result base(BaseException baseException) {
        Result result = new Result();
        result.setCode(ResultCode.FAIL.code);
        result.setMessage(baseException.getMessage());
        return result;
    }

    @ExceptionHandler(SQLException.class)
    public Result sql() {
        Result result = new Result();
        result.setCode(ResultCode.INTERNAL_SERVER_ERROR.code);
        result.setMessage(ResultCode.INTERNAL_SERVER_ERROR.message);
        return result;
    }

    @ExceptionHandler(Exception.class)
    public Result exception() {
        Result result = new Result();
        result.setCode((ResultCode.INTERNAL_SERVER_ERROR.code));
        result.setMessage(ResultCode.INTERNAL_SERVER_ERROR.message);
        return result;
    }
}
